<template>
  <el-dialog
    title="上传文件"
    :close-on-click-modal="false"
    @close="closeHandle"
    :visible.sync="visible">
    <single-upload v-model="url"></single-upload>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="cancelSubmit()">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import singleUpload from '../../../components/upload/singleUpload'
  export default {
    components: {
      singleUpload
    },
    data () {
      return {
        visible: false,
        url: ''
      }
    },
    methods: {
      init (id) {
        this.visible = true
      },
      // 表单提交
      dataFormSubmit () {
        if (this.url) {
          this.$http({
            url: this.$http.adornUrl(`/sys/oss/add`),
            method: 'post',
            data: this.$http.adornData({
              'url': this.url
            })
          }).then(({data}) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          this.$message.error('图片不能为空')
        }
      },
      cancelSubmit () {
        this.visible = false
      },
      // 弹窗关闭时
      closeHandle () {
        this.$emit('refreshDataList')
      }
    }
  }
</script>
